package com.gpoint.rdp.door.zonepermission.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.common.DoorConfig;
import com.gpoint.rdp.door.zonepermission.service.ZonePermissionService;
import com.gpoint.rdp.door.zonepermission.service.ZonePermissionQueryService;
import com.gpoint.rdp.door.TimeSegment.persistence.entity.TimeSegPo;
import com.gpoint.rdp.door.zonepermission.persistence.dao.ZonePermissionDao;
import com.gpoint.rdp.door.zonepermission.persistence.dao.ZonePermissionQueryDao;
import com.gpoint.rdp.door.zonepermission.persistence.entity.ZonePermissionPo;
import com.gpoint.rdp.door.zonepermission.repository.ZonePermissionRepository;
import com.gpoint.rdp.base.core.util.AppUtil;
import com.gpoint.rdp.base.core.util.BeanUtils;
import com.gpoint.rdp.base.core.util.JacksonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 区域权限表 控制类
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-11-14 09:11:54
 *</pre>
 */
@Controller
@RequestMapping("/rdp/zonepermission/zonePermission/")
public class ZonePermissionController extends GenericController{
	@Resource
	private ZonePermissionService zonePermissionService;
	@Resource
	private ZonePermissionQueryService zonePermissionQueryService;
	
	
//	@Resource
//	private ZonePermissionRepository zonePermissionRepository;
	
	
	
//	@Resource
//	private ZonePermissionDao zonePermissionDao;
	
	/**
	 * 【区域权限表】列表(分页条件查询)数据
	 *
	 * @param request
	 * @param reponse
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("listJson")
	public @ResponseBody PageJson listJson(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		QueryFilter queryFilter = getQuerFilter(request);
		try{
			String listData = zonePermissionQueryService.query(queryFilter);
			PageList<ZonePermissionPo> zonePermissionList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<ZonePermissionPo> list = ZonePermissionPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				zonePermissionList = new PageList<ZonePermissionPo>(list, pageResult);
			}
			
			return new PageJson(zonePermissionList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	
	
	//根据账户，查询对应账户的区域
	@RequestMapping("getZone")
	public @ResponseBody List<ZonePermissionPo> getTreeData(HttpServletRequest request, HttpServletResponse reponse)
			throws Exception {
		try {
			
			String  account=RequestUtil.getString(request, "account");
			List<ZonePermissionPo> list=    this.zonePermissionQueryService.getZone(account);
			
			if (BeanUtils.isEmpty(list)) {
				list = new ArrayList<ZonePermissionPo>();
			}

			return list;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 编辑【区域权限表】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		ZonePermissionPo zonePermission=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = zonePermissionQueryService.get(id);
			zonePermission = ZonePermissionPo.fromJsonString(data);
		}

		return getAutoView().addObject("zonePermission", zonePermission).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 编辑【区域权限表】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		ZonePermissionPo zonePermission=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = zonePermissionQueryService.get(id);
			zonePermission = ZonePermissionPo.fromJsonString(data);
		}
		return getAutoView().addObject("zonePermission", zonePermission).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 【区域权限表】明细页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		ZonePermissionPo zonePermission=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = zonePermissionQueryService.get(id);
			zonePermission = ZonePermissionPo.fromJsonString(data);
		}
		return getAutoView().addObject("zonePermission", zonePermission).addObject("returnUrl", preUrl);
	}
	DoorConfig config=AppUtil.getBean(DoorConfig.class);
    String servicecode=config.getServicecode();
	/** 
	 * 保存【区域权限表】信息
	 *
	 * @param request
	 * @param response
	 * @param  zonePermission
	 * @throws Exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			String account=RequestUtil.getString(request,"account");
			String idstr=RequestUtil.getString(request,"idstr");
			String status="0";
			 
			//异常判断
			/*
			 * if(BeanUtils.isEmpty(account) ||BeanUtils.isEmpty(idstr)) { message=new
			 * ResultMessage(ResultMessage.FAIL, "获取数据为空");
			 * 
			 * message=new ResultMessage(ResultMessage.SUCCESS, "保存数据为空"); return; }
			 */
			 //先清空再保存
			 
			 
			 //1.直接删除
	     	 String[] acc=RequestUtil.getStringAryByStr(request, "account");
	     	 if(acc.length>0) {
	     		 for (int i = 0; i < acc.length; i++) {
//		     		  this.zonePermissionDao.deleteByKey("removebyaccount", ACCOUNT[i]);
				       this.zonePermissionService.removebyaccount(acc[i]);
	     		 }
			      
	     	 }
	     	  
			 
			 //2.保存
			 saveSimpleObj(account, idstr, status);
		 
		
			 
			 
			 // 这里account是是主键， 查出id 是否存在，存在update,不存在插入
//			 List<ZonePermissionPo> idpo = zonePermissionQueryDao.findByKey("getIdcount",account);
//			 if(idpo.size()>0) { //更新 只可能有一条
//				 
//				 //查询出ID
//				    String id = idpo.get(0).getId();
//				            
//				 ZonePermissionPo po=new ZonePermissionPo();
//				 po.setId(id);
//				 po.setAccount(account);
//				 po.setServicecode(servicecode);
//				 po.setZoneid(idstr);
//				 po.setStatus(status);
//				 String jsonData=JacksonUtil.toJsonString(po);
//				 zonePermissionService.save(jsonData);
//			 
//				 //查询出update:设置id
//			 }else { //插入
//				 ZonePermissionPo po=new ZonePermissionPo();
//				 po.setAccount(account);
//				 po.setServicecode(servicecode);
//				 po.setZoneid(idstr);
//				 po.setStatus(status);
//				 String jsonData=JacksonUtil.toJsonString(po);
//				 zonePermissionService.save(jsonData);
//			 }
			 
			 
			
			 
			 
			 
			 
			 
			message=new ResultMessage(ResultMessage.SUCCESS, "保存区域权限表成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "对区域权限表操作失败,"+e.getMessage());
			logger.error("对区域权限表操作失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}


	private void saveSimpleObj(String account, String idstr, String status) {
		ZonePermissionPo zonePermissionPo=new ZonePermissionPo();
		  zonePermissionPo.setAccount(account);
		  zonePermissionPo.setServicecode(servicecode);
		  zonePermissionPo.setStatus(status);
		  zonePermissionPo.setZoneid(idstr);
//		zonePermissionRepository.newInstance(zonePermissionPo).create();  //这里有多条记录
	  this.zonePermissionService.create(zonePermissionPo);
	}
	
	/**
	 *  批量删除【区域权限表】记录
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			//获得待删除的id
			String[] ids=RequestUtil.getStringAryByStr(request, "id");
			zonePermissionService.deleteByIds(ids);
			message=new ResultMessage(ResultMessage.SUCCESS, "删除区域权限表成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "删除区域权限表失败，" + e.getMessage());
			logger.error("删除区域权限表失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	

}